package com.heima.admin.controller.v1;

import com.heima.admin.service.AdChannelService;
import com.heima.api.admin.ChannelControllerApi;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.validator.HeimaValidatorAdd;
import com.heima.model.validator.HeimaValidatorUpdate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理",tags = "channel",description = "频道管理Api")
public class AdChannelController implements ChannelControllerApi {

    @Autowired
    AdChannelService adChannelService;

    /**
     * 根据名称分页查询频道列表信息
     * @param channelDto
     * @return
     */
    @PostMapping("/list")
    @ApiOperation("频道名称分页查询列表")
    public ResponseResult findByNameAndPage(@RequestBody ChannelDto channelDto){
        return adChannelService.findByNameAndPage(channelDto);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        return adChannelService.delete(id);
    }

    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    @GetMapping("/channels")
    public ResponseResult findAll() {

        return ResponseResult.okResult(adChannelService.list());
    }

    /**
     * 修改
     *
     * @param adChannel
     * @return
     */
    @Override
    @PutMapping("/update")
    public ResponseResult update(@RequestBody @Validated({HeimaValidatorUpdate.class}) AdChannel adChannel) {
        return adChannelService.update(adChannel);
    }

    /**
     * 新增
     *
     * @param adChannel
     * @return
     */
    @PostMapping("/save")
    public ResponseResult insert(@RequestBody @Validated({HeimaValidatorAdd.class}) AdChannel adChannel) {
        return adChannelService.insert(adChannel);
    }
}
